package com.rfid.emergencywarehouse.net;

import com.google.gson.GsonBuilder;
import com.rfid.base.manager.TokenManager;
import com.rfid.base.util.LogUtils;
import com.rfid.base.util.PreferenceUtil;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.manager.UserInfoManager;
import com.rfid.emergencywarehouse.net.entity.AssetOrderRes;
import com.rfid.emergencywarehouse.net.entity.CreateListingReq;
import com.rfid.emergencywarehouse.net.entity.CreateListingRes;
import com.rfid.emergencywarehouse.net.entity.CreateStockReq;
import com.rfid.emergencywarehouse.net.entity.CreateStocklRes;
import com.rfid.emergencywarehouse.net.entity.EnterWorkArea;
import com.rfid.emergencywarehouse.net.entity.GetAssetReq;
import com.rfid.emergencywarehouse.net.entity.GetAssetRes;
import com.rfid.emergencywarehouse.net.entity.GetMaterialCategoryListRes;
import com.rfid.emergencywarehouse.net.entity.KFKWRes;
import com.rfid.emergencywarehouse.net.entity.LoginReq;
import com.rfid.emergencywarehouse.net.entity.LoginRes;
import com.rfid.emergencywarehouse.net.entity.MaterialModeRes;
import com.rfid.emergencywarehouse.net.entity.MaterilListRes;
import com.rfid.emergencywarehouse.net.entity.GetOrderDetailRes;
import com.rfid.emergencywarehouse.net.entity.OrderKwRes;
import com.rfid.emergencywarehouse.net.entity.OrderRes;
import com.rfid.emergencywarehouse.net.entity.PanDianReq;
import com.rfid.emergencywarehouse.net.entity.PandianRes;
import com.rfid.emergencywarehouse.net.entity.QueryBarcodeReq;
import com.rfid.emergencywarehouse.net.entity.QueryBarcodeRes;
import com.rfid.emergencywarehouse.net.entity.RackRes;
import com.rfid.emergencywarehouse.net.entity.ReturnReq;
import com.rfid.emergencywarehouse.net.entity.ReturnRes;
import com.rfid.emergencywarehouse.net.entity.ShelfingReq;
import com.rfid.emergencywarehouse.net.entity.ShelfingRes;
import com.rfid.emergencywarehouse.net.entity.StockBarcodeRes;
import com.rfid.emergencywarehouse.net.entity.UserInfoRes;
import com.rfid.emergencywarehouse.net.entity.WGStatusRes;
import com.rfid.emergencywarehouse.net.entity.WareHouseareaRes;
import com.rfid.emergencywarehouse.net.entity.WorkAreRes;
import com.rfid.emergencywarehouse.net.entity.WorkAreaRes;
import com.rfid.emergencywarehouse.util.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public enum NetApi {

    INSTANCE;
    INetService mApiService;
    //http://192.168.0.59:8003
    public static String DEFAULT_URL = "http://58.33.104.58:41824";

    public void init() {
        try {
            String url = PreferenceUtil.getString("url",DEFAULT_URL);
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
                    .client(OkHttpUtils.getClient(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request origin = chain.request();
                            if (TokenManager.INSTANCE.getToken() != null) {
                                Request request = origin.newBuilder()
                                        .addHeader("Authorization", "Bearer  " + TokenManager.INSTANCE.getToken())
                                        .build();
                                return chain.proceed(request);
                            }
                            return chain.proceed(origin);
                        }
                    }))
                    .build();
            mApiService = retrofit.create(INetService.class);
        } catch (Exception e) {
            LogUtils.e(e);
        }
    }

    public LoginRes login(String name, String pwd) throws IOException {
        LoginReq req = new LoginReq();
        req.setApplicationId("GH012");
        req.setUsername(name);
        req.setPassword(pwd);
        LoginRes res = mApiService.login(req).execute().body();
        if (res.getData() != null) {
            long expires = res.getData().getExpires();
            System.out.println(expires);
            long now = System.currentTimeMillis();
            System.out.println(now);
            TokenManager.INSTANCE.setToken(res.getData().getAccessToken(), expires - now / 1000);
        }
        return res;
    }

    public UserInfoRes getLoginUserInfo() throws IOException {
        return mApiService.getUserInfo().execute().body();
    }

    public MaterilListRes getMaterialList(String categoryId) throws IOException {
        return mApiService.getMaterialList(categoryId).execute().body();
    }

    public MaterialModeRes getMaterialModeList(String materialId) throws Exception {
        return mApiService.getMaterialModeList(materialId).execute().body();
    }

    public GetMaterialCategoryListRes getMaterialCategoryListRes() throws Exception {
        return mApiService.getMaterialCategoryList(1,10000).execute().body();
    }

    public CreateStocklRes addStock(List<String> barcodes, String materialId, String skuId,String shapeStatus) throws IOException {
        CreateStockReq req = new CreateStockReq();
        req.setMaterialId(materialId);
        req.setSkuId(skuId);
        List<CreateStockReq.ItemsBean> itemsBeans = new ArrayList<>();
        for (String barcode:barcodes) {
            CreateStockReq.ItemsBean itemsBean = new CreateStockReq.ItemsBean();
            itemsBean.setBarCode(barcode);
            itemsBean.setShapeStatus(shapeStatus);
            itemsBeans.add(itemsBean);
        }
        req.setItems(itemsBeans);
        return mApiService.createStock(req).execute().body();
    }

    public StockBarcodeRes getStockList() throws IOException {
        return mApiService.getStockList().execute().body();
    }

    public WorkAreaRes getWorkArea() throws IOException {
        return mApiService.getWorkAreaList().execute().body();
    }

    public RackRes getRackList() throws IOException {
        return mApiService.getRackList().execute().body();
    }

    public CreateListingRes createListing(String toWorkAreaId,List<String> barcodes) throws IOException {
        CreateListingReq req = new CreateListingReq();
        req.setToWorkAreaId(toWorkAreaId);
        List<String> data = new ArrayList<>();
        for (String b : barcodes) {
            data.add(b);
        }
        req.setStockBarCodes(data);
        return mApiService.createListing(req).execute().body();
    }

    public OrderRes getOrderList() throws IOException {
        return mApiService.getOrderList(30, UserInfoManager.INSTANCE.getUserId()).execute().body();
    }

    public ShelfingRes shelfing(String location,List<String> barcodes) throws IOException {
        ShelfingReq req = new ShelfingReq();
        req.setLocationId(location);
        req.setBarCodes(barcodes);
        return mApiService.shelfing(req).execute().body();
    }

    public QueryBarcodeRes queryBarcodes(List<String> barcodes) throws IOException {
        QueryBarcodeReq req = new QueryBarcodeReq();
        req.setBarCodes(barcodes);
        return mApiService.queryBarcodeInfo(req).execute().body();
    }

    public WGStatusRes getWGStatusConfig() throws IOException {
        return mApiService.getWGStatusConfig("Material.ShapeStatus.List").execute().body();
    }

    public ReturnRes returnAsset(List<BarcodeInfo> barcodes) throws IOException {
        ReturnReq req = new ReturnReq();
        List<ReturnReq.ItemsBean> beans = new ArrayList<>();
        for (BarcodeInfo barcode : barcodes) {
            ReturnReq.ItemsBean b = new ReturnReq.ItemsBean();
            b.setBarCode(barcode.getBarcode());
            b.setEnabled(barcode.isEnable());
            b.setShapeStatus(barcode.getShapeStatus());
            beans.add(b);
        }
        req.setItems(beans);
        return mApiService.returnAsset(req).execute().body();
    }

    public GetAssetRes doGetAsset(String orderId,List<String> barcodes) throws IOException {
        GetAssetReq req = new GetAssetReq();
        req.setBarCodes(barcodes);
        req.setId(orderId);
        return mApiService.doGetAsset(req).execute().body();
    }

    public AssetOrderRes getAssetOrderList() throws IOException {
        return mApiService.getAssetOrderList(30, UserInfoManager.INSTANCE.getUserId()).execute().body();
    }

    public WorkAreRes getKf(String outOrderId) throws IOException {
        return mApiService.getWorkAre(outOrderId).execute().body();
    }

    public KFKWRes getKw(String OutOrderId,String fromWorkAreaId) throws IOException {
        return mApiService.getKFKWOfOrder(OutOrderId,fromWorkAreaId).execute().body();
    }

    public GetOrderDetailRes getGetOrderDetail(String id) throws IOException {
        return mApiService.getGetOrderDetail(id).execute().body();
    }

    public OrderKwRes getInOrderKw(String orderId) throws IOException {
        return mApiService.getOrderKw(orderId).execute().body();
    }

    public PandianRes pandian(String sid, List<String> barcodes) throws IOException {
        PanDianReq req = new PanDianReq();
        req.setAisleId(sid);
        req.setBarCodes(barcodes);
        return mApiService.pandian(req).execute().body();
    }

    public ReturnRes postEnterStatus(EnterWorkArea req) throws IOException {
        return mApiService.enterWorkArea(req).execute().body();
    }

    public ReturnRes exitWorkArea(EnterWorkArea req) throws IOException {
        return mApiService.exitWorkArea(req).execute().body();
    }
}
